import sys
from PySide6.QtWidgets import (
    QApplication,
    QDialog,
    QFrame,
    QWidget,
    QStackedWidget,
    QVBoxLayout,
    QHBoxLayout,
    QLabel,
    QStyle,
    QToolBar,
)
from PySide6.QtCore import Qt, QSize
from PySide6.QtGui import QIcon, QPixmap, QImage, QAction
from hedra import devel_hedra


class ScanQrcodeLogin(QWidget):
    """默认布局"""

    def __init__(self, name="", parent=None):
        super(ScanQrcodeLogin, self).__init__(parent)
        self.name = name
        self.setUI()
        self.setBackgroundColor()
        self.setText("系统初始化...")

    def setUI(self):
        self.qicon_1 = QLabel()
        self.label_1 = QLabel("")
        h = QHBoxLayout(self)
        h.setContentsMargins(0, 0, 0, 0)
        h.addWidget(self.qicon_1)
        h.addWidget(self.label_1)

    def setBackgroundColor(self) -> None:
        """
        QStyle.SP_MessageBoxInformation
        QStyle.SP_MessageBoxWarning
        QStyle.SP_MessageBoxCritical
        QStyle.SP_MessageBoxQuestion
        """
        if self.name == "debug":
            icon = QIcon(
                QApplication.style().standardIcon(QStyle.SP_MessageBoxQuestion)
            )
            Color = "color: rgba(255, 255, 255,1 );"
        elif self.name == "info":
            icon = QIcon(
                QApplication.style().standardIcon(QStyle.SP_MessageBoxInformation)
            )
            Color = "color: rgba(0, 255, 0,1);"
        elif self.name == "warn":
            icon = QIcon(QApplication.style().standardIcon(QStyle.SP_MessageBoxWarning))
            Color = "color: rgba(255, 255, 0,1);"
        elif self.name == "error":
            icon = QIcon(
                QApplication.style().standardIcon(QStyle.SP_MessageBoxCritical)
            )
            Color = "color: rgba(255, 0, 0,1);"
        else:
            icon = QIcon(
                QApplication.style().standardIcon(QStyle.SP_MessageBoxCritical)
            )
            Color = "color: rgba(200, 200, 200,1);"
        pixmap = icon.pixmap(icon.availableSizes()[0])
        self.qicon_1.setPixmap(pixmap)
        self.qicon_1.setFixedSize(pixmap.size())
        self.setStyleSheet(Color)

    def setText(self, text):
        self.label_1.setText(text)


class MainChangeWindow(QStackedWidget):
    """全局布局"""

    tap = ["debug", "info", "warn", "error"]

    def __init__(self, parent=None):
        super(MainChangeWindow, self).__init__(parent)
        self.taps = [self.addWidget(ScanQrcodeLogin(i)) for i in self.tap]
        self.setCurrentIndex(1)

    def changeIndex(self, level, text):
        self.setCurrentIndex(self.tap.index(level))
        self.currentWidget().setText(text)


class mainActionMenuToolBar(QToolBar):
    """全局工具栏"""

    def __init__(self, idx=1, parent=None):
        super().__init__()
        self.setIconSize(QSize(16, 16))
        self.setupUI(idx)
        self.setStyleSheet("color: rgba(250, 250, 250, 1);")

    def setupUI(self, idx):
        if not idx:
            self.addAction(
                QAction(
                    QApplication.style().standardIcon(QStyle.SP_TitleBarCloseButton),
                    "关闭",
                    self,
                )
            )
        else:
            self.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
            b1 = QAction(
                QApplication.style().standardIcon(QStyle.SP_VistaShield),
                "Demo 01",
                self,
            )
            b1.setCheckable(True)
            b2 = QAction(
                QApplication.style().standardIcon(QStyle.SP_DirHomeIcon),
                "Demo 02",
                self,
            )
            b2.setCheckable(True)
            b3 = QAction(
                QApplication.style().standardIcon(QStyle.SP_DesktopIcon),
                "Demo 03",
                self,
            )
            b3.setCheckable(True)
            self.addAction(b1)
            self.addSeparator()
            self.addAction(b2)
            self.addSeparator()
            self.addAction(b3)


class MainWindow(QDialog):
    def __init__(self, parent=None):
        super().__init__()
        # self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlags(
            Qt.FramelessWindowHint | Qt.Window | Qt.WindowStaysOnTopHint
        )
        self.setStyleSheet("background-color: rgba(80, 80, 80, 1);")
        self.tool = mainActionMenuToolBar()
        self.ened = mainActionMenuToolBar(0)
        self.Xbox = MainChangeWindow()
        # 行一
        h0 = QHBoxLayout()
        h0.setContentsMargins(0, 0, 0, 0)
        h0.addWidget(self.tool)
        h0.addStretch(1)
        h0.addWidget(self.ened)
        # 顶层
        v = QVBoxLayout(self)
        v.setContentsMargins(5, 5, 5, 5)
        v.addLayout(h0)
        v.addWidget(self.Xbox)

        # print(self.Xbox.taps)
        # self.Xbox.changeIndex("error", "测试消息")
        # self.msg = devel_hedra()
        # self.msg.do_test_console()
        # self.msg.do_main_console()
        # 信号槽
        self.ened.actionTriggered.connect(self.MSG_SYSTEM)
        self.tool.actionTriggered.connect(self.MSG_SYSTEM)

    def closeEvent(self, event):
        print("关闭事件")
        if self.msg:
            self.msg.close()
        event.accept()  # event.ignore()

    def MSG_SYSTEM(self, e):
        if e.text() == "关闭":
            self.close()
        if e.text() == "Demo 01":
            res = ["res/nv01.jpg", "res/fengxin5.mp3"]
            self.msg = devel_hedra()
            self.msg.do_main_console()
            self.msg.uploadRes(res)
        if e.text() == "Demo 02":
            res = ["res/nv00.jpg", "res/fengxin5.mp3"]
            self.msg.uploadRes(res)


if __name__ == "__main__":
    print("Hello")
    app = QApplication(sys.argv)
    mainwindow = MainWindow()
    # mainwindow.setWindowTitle("YaoDB")
    mainwindow.setGeometry(0, 0, 360, 80)
    mainwindow.show()
    sys.exit(app.exec())
